package com.example.homework

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Face
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.outlined.Face
import androidx.compose.material.icons.outlined.LocationOn
import androidx.compose.material.icons.outlined.PlayArrow
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.example.homework.ui.theme.HomeworkTheme
import com.example.homework.utils.BottomNavigationItem
import com.example.homework.ui.screens.CharacterScreen
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import com.example.homework.ui.screens.CharacterInfoScreen
import com.example.homework.ui.screens.EpisodesScreen
import com.example.homework.ui.screens.LocationsScreen

class MainActivity : ComponentActivity() {
    @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            HomeworkTheme {
                val navigationItems: List<BottomNavigationItem> = listOf(
                    BottomNavigationItem(
                        "Characters",
                        Icons.Filled.Face,
                        Icons.Outlined.Face,
                        CharacterScreen
                    ),
                    BottomNavigationItem(
                        "Locations",
                        Icons.Filled.LocationOn,
                        Icons.Outlined.LocationOn,
                        LocationsScreen
                    ),
                    BottomNavigationItem(
                        "Episodes", Icons.Filled.PlayArrow, Icons.Outlined.PlayArrow, EpisodesScreen
                    ),
                )
                var selectedItemIndex by rememberSaveable {
                    mutableStateOf(0)
                }
                val navController = rememberNavController()

                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
                    Scaffold(
                        bottomBar = {
                            NavigationBar {
                                navigationItems.forEachIndexed { index, item ->
                                    NavigationBarItem(
                                        selected = selectedItemIndex == index,
                                        onClick = {
                                            selectedItemIndex = index
                                            navController.navigate(item.screen)
                                        },
                                        label = {
                                            Text(item.title)
                                        },
                                        icon = {
                                            Icon(
                                                imageVector = if (index == selectedItemIndex) item.selectedIcon
                                                else item.unselectedIcon,
                                                contentDescription = item.title
                                            )
                                        })
                                }
                            }
                        }) { paddingValues ->
                        Column(modifier = Modifier.padding(paddingValues)) {
                            NavHost(
                                navController, startDestination = CharacterScreen
                            ) {
                                composable<CharacterScreen> {
                                    CharacterScreen.Body(
                                        navController = navController
                                    )
                                }
                                composable<LocationsScreen> { LocationsScreen.Body() }
                                composable<EpisodesScreen> { EpisodesScreen.Body() }
                                composable<CharacterInfoScreen> { CharacterInfoScreen(it.toRoute<CharacterInfoScreen>().id).Body() }
                            }
                        }
                    }
                }
            }
        }
    }
}


@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!", modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    HomeworkTheme {
        Greeting("Android")
    }
}